﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SaturniLibrary.MIGS.V1
{
    public class MIGSHelper
    {
        public static string GetSearchHash(string secureHash, SortedDictionary<string, string> fields)
        {
            secureHash = fields.Aggregate(secureHash, (current, field) => current + field.Value);
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(secureHash);
            var md5 = new MD5CryptoServiceProvider();
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            var sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
                // To force the hex string to lower-case letters instead of
                // upper-case, use he following line instead:
                // sb.Append(hashBytes[i].ToString("x2")); 
            }
            return sb.ToString();
        }
    }
}
